package extractor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import utils.Utils;

public class CategoryCoecHelper {
	HashMap<String, float[]> categoryMap = new HashMap<String, float[]>();
	public void init(FileSystem fs, Path path, Configuration conf) throws IOException{
		categoryMap.clear();
		FileStatus[] srcs = fs.globStatus(path);
		for (int i = 0; i < srcs.length; ++i) {
			if (srcs[i].isDir())
				continue;
			Path p = srcs[i].getPath();		
			BufferedReader reader = new BufferedReader(new InputStreamReader(fs.open(p)));			
			
			String line;
			while((line = reader.readLine()) != null) {
				String[] values = line.split(Utils.separator);
				categoryMap.put(values[0], new float[]{Float.parseFloat(values[1]), Float.parseFloat(values[2])});
			}
			reader.close();		
		}
	}
	
	public float getCategoryCoec(String category) {
		if (categoryMap.containsKey(category))
			return categoryMap.get(category)[0];
		return -1;
	}
	
	public float getCategoryCtr(String category) {
		if (categoryMap.containsKey(category))
			return categoryMap.get(category)[1];
		return -1;
	}
	
	public String getCategoryCoecCtrStr(String category) {
		if (categoryMap.containsKey(category)) {
			float[] info = categoryMap.get(category);
			return Utils.getRecord(new String[]{String.valueOf(info[0]), String.valueOf(info[1])}, Utils.separator);
		}
			
		return "-1\t-1";
	}
}
